var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: '65ZBZ-KRVKP-ID4D5-VSTIE-RIAAZ-TQBRO'
});
var city = require('city.js')
var lineHeight = 0;
var endWords = "";
var isNum;
Page({
  data: {
    "hidden": true,//选中显示 侧边连
    cityName: "", //获取选中的城市名
    searchArr:[],//搜索的数组
  },
  yes_but(){
    var that = this
    var cityName = that.data.cityName
    // 村缓存 显示城市city_danqian
    wx.setStorageSync('cityName',cityName)
    wx.navigateBack({
      delta: 1,
    })
  },
  onLoad: function (options) {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success (res) {//用户位置经纬度
        that.setData({
          mylat:res.latitude,//定位用的经纬度
          mylon:res.longitude,//定位用的经纬度
        })
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) {
            console.log(res)
            that.setData({city_danqian:res.result.address_component.city})
            // // 村缓存 显示城市city_danqian
            // wx.setStorageSync('cityName', res.result.address_component.city)
          }
        })
      },
      fail(res){
        wx.showToast({
          title: "请开启GPS定位使用地图",
          icon: 'none',
          duration: 1500,
          mask: true
        })
      }
    })
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    var cityChild = city.City[0];
    console.log(cityChild)
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        lineHeight = (res.windowHeight - 100) / 22;
        console.log(res.windowHeight - 100)
        that.setData({
          city: cityChild,
          winHeight: res.windowHeight - 40,
          lineHeight: lineHeight
        })
      }
    })
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
  },
  // 回车
  searchBut(e){
    var that = this
    var val = e.detail.value
    var city = that.data.city
    console.log(val)
    console.log(city)
    if(val == ''){
      
      return false
    }
    var arr = []
    for(let i in city){
      for(let k in city[i]){
        // console.log(city[i][k])
        if(city[i][k].name.indexOf(val)>=0){
          // console.log(city[i][k].name)
          arr.push(city[i][k].name)
        }
      }
    }

    if(arr.length > 0){
      console.log(arr)
      that.setData({searchArr:arr})
    }else{
      wx.showToast({
        title: '无数据',
        icon:'none',  
      })
      that.setData({searchArr:[]})
    }

  },
  //触发全部开始选择
  chStart: function () {
    this.setData({
      // trans: ".3",
      trans: "",
      hidden: false
    })
  },
  //触发结束选择
  chEnd: function () {
    this.setData({
      trans: "0",
      hidden: true,
      scrollTopId: this.endWords
    })
  },
  //获取文字信息
  getWords: function (e) {
    console.log(e)
    var id = e.target.id;
    this.endWords = id;
    isNum = id;
    this.setData({
      showwords: this.endWords
    })
  },
  //设置文字信息
  setWords: function (e) {
    var id = e.target.id;
    this.setData({
      scrollTopId: id
    })
  },
 
  // 滑动选择城市
  chMove: function (e) {
    var y = e.touches[0].clientY;
    var offsettop = e.currentTarget.offsetTop;
    var height = 0;
    var that = this;;
    var cityarr = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"]
    // 获取y轴最大值
    wx.getSystemInfo({
      success: function (res) {
        height = res.windowHeight - 10;
      }
    });
 
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop && y < height) {
      // console.log((y-offsettop)/lineHeight)
      var num = parseInt((y - offsettop) / lineHeight);
      endWords = cityarr[num];
      // 这里 把endWords 绑定到this 上，是为了手指离开事件获取值
      that.endWords = endWords;
    };
 
 
    //去除重复，为了防止每次移动都赋值 ,这里限制值有变化后才会有赋值操作，
    //DOTO 这里暂时还有问题，还是比较卡，待优化
    if (isNum != num) {
      // console.log(isNum);
      isNum = num;
      that.setData({
        showwords: that.endWords
      })
    }
  },
  //选择城市，并让选中的值显示在文本框里
  bindCity: function (e) {
    var cityName = e.currentTarget.dataset.city;
console.log(cityName)
    this.setData({
      cityName: cityName,
      // city_danqian:cityName
    })
    // var pages = getCurrentPages();
    // var prevPage = pages[pages.length - 2]; //上一个页面
    // console.log(cityName)
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    // wx.setStorageSync('cityName', cityName)
    // wx.navigateBack()
    // wx.navigateBack({
    //   url: '/pages/home/search/index'
    // })
  }
})